<?php
namespace app\api\controller\xilujob;


use app\common\controller\xilujob\XilujobApi;
use app\common\model\xilujob\PersonalMessage;
use app\common\model\xilujob\XilujobUser;
use app\index\model\xilujob\SystemMessage;

class Message extends XilujobApi {

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];
    protected $restrictedActions = ['*'];

    /**
     * @ApiTitle (消息概况)
     * @ApiSummary (消息总的统计情况)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.message/summary)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function summary() {
        $user = $this->auth->getUser();
        $xilujob_user = XilujobUser::get(['user_id'=>$user->id]);
        $where = [];
        if($xilujob_user['group_id'] != 1){
            $where['company_id'] = $xilujob_user->group_model->id;
        }else{
            $where['user_id'] = $this->auth->id;
            $where['user_group_id'] = 1;
        }
        $personalMessage = PersonalMessage::where($where)
            ->order('id', 'desc')
            ->find();
        $activityMessage = SystemMessage::where('type', SystemMessage::TYPE_ACTIVITY)
            ->order('id', 'desc')
            ->find();
        $systemMessage = SystemMessage::where('type', SystemMessage::TYPE_SYSTEM)
            ->order('id', 'desc')
            ->find();
        $field = $xilujob_user['group_id'] == 1 ? 'personal_message' : 'discover_message';
        $data = [
            'personal'=>['total'=>$xilujob_user->$field,'message'=>$personalMessage],
            'activity'=>['total'=>$xilujob_user->activity_message,'message'=>$activityMessage],
            'system'=>['total'=>$xilujob_user->system_message,'message'=>$systemMessage]
        ];
        $this->success('查询成功',$data);
    }

    /**
     * @ApiTitle (个人消息)
     * @ApiSummary (个人消息列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.message/personal_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function personal_list() {
        $user  =$this->auth->getUser();
        $xilujob_user = XilujobUser::get(['user_id'=>$user->id]);
        $where = [];
        if($xilujob_user['group_id'] != 1){
            $where['company_id'] = $xilujob_user->group_model->id;
        }else{
            $where['user_id'] = $this->auth->id;
            $where['user_group_id'] = 1;
        }
        $field = $xilujob_user['group_id'] == 1 ? 'personal_message' : 'discover_message';
        if($xilujob_user->$field>0) {
            $xilujob_user->save([$field=>0]);
        }
        $list = PersonalMessage::where($where)
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功', ['personal_messages'=>$list->getCollection()]);
    }

    /**
     * @ApiTitle (活动消息)
     * @ApiSummary (活动消息列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.message/activity_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function activity_list() {
        $user  =$this->auth->getUser();
        $xilujob_user = XilujobUser::get(['user_id'=>$user->id]);
        if($xilujob_user->activity_message>0) {
            $xilujob_user->save(['activity_message'=>0]);
        }
        $list = SystemMessage::where('type', SystemMessage::TYPE_ACTIVITY)
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功', ['activity_messages'=>$list->getCollection()]);
    }

    /**
     * @ApiTitle (系统消息)
     * @ApiSummary (系统消息列表)
     * @ApiMethod (POST)
     * @ApiRoute (/api/xilujob.message/system_list)
     * @ApiHeaders (name=token, type=string, require=true, description="Token")
     */
    public function system_list() {
        $user  =$this->auth->getUser();
        $xilujob_user = XilujobUser::get(['user_id'=>$user->id]);
        if($xilujob_user->system_message>0) {
            $xilujob_user->save(['system_message'=>0]);
        }
        $list = SystemMessage::where('type', SystemMessage::TYPE_SYSTEM)
            ->order('id','desc')
            ->paginate();
        $this->success('查询成功', ['system_messages'=>$list->getCollection()]);
    }

}